invoiceDialogMode=0;

function invoiceIndexLoad(){
  $("#menuInvoiceNew").click(
    function(e){
      menuSelect("#invoiceMenuBar",this);
      $.get(this.href,{},
        function(content){
          $("#ajMain").show().html(content);
          $("#ajContactPanel").hide();
          $("#ajContactForm").remove();
          contactDialogMode=10;
        }
      );
      e.preventDefault();
    }
  );
  $("#menuInvoiceSearch").click(
    function(e){
      menuSelect("#invoiceMenuBar",this);
      $.get(this.href,{},
        function(content){
          $("#ajMain").show().html(content);
          $("#ajContactPanel").hide();
          $("#ajContactForm").remove();
        }
      );
      e.preventDefault();
    }
  );
}

function getGridRowData(invoice_iden){
  $.get(
    'invoice/getInvoiceTd',{'invoice_iden':invoice_iden},
    function(content){
      $('#row_invoice_'+invoice_iden).html(content);
      $('#link_invoice_'+invoice_iden).click(selectInvoice);
    }
  );
}

function invoiceFormSubmit(){
  showStatusWindow();
  $('#formInvoice').ajaxSubmit({
    type: 'post',
    dataType: 'html',
    success: 
      function(data,statusText){
        $('#ajInvoicePanel').html(data);
        hideStatusWindow();
      },
  });
}

function invoiceSetupDeleteButton(url,param){
  $("#buttonDeleteInvoice").click(
    function(){
      showStatusWindow();
      $.post(url,param,
        function(content){
          $("#ajInvoicePanel").html(content);
          hideStatusWindow();
        }
      );
    }
  );
}


function invoiceSetupStatusButton(url,param){
  $("#buttonStatusInvoice").click(
    function(){
      showStatusWindow();
      $.post(url,param,
        function(content){
          $("#ajInvoicePanel").html(content);
          hideStatusWindow();
        }
      );
    }
  );
}

function invoiceSetupResetButton(url,param){
  $("#buttonResetInvoice").click(
    function(){
      showStatusWindow();
      $.post(url,param,
        function(content){
          $("#ajInvoicePanel").html(content);
          hideStatusWindow();
        }
      );
    }
  );
}

function invoiceSetupSearchForm(){
  $("#formInvoiceSearch").submit(
    function(){
      showStatusWindow();
      
      $(this).ajaxSubmit(
        {
          type: 'get',
          dataType: 'html',
          success: 
            function(data,statusText){
              $('#ajInvoiceGrid').show().html(data);
              $('#ajInvoicePanel').hide();
              $('#ajInvoiceShowGrid').hide();
              $('#ajInvoiceForm').remove();
              hideStatusWindow();
            },
        }
      );
      
      return false;
    }
  );
}

function invoiceSetupShowSearchGrid(){
  $('#ajInvoiceShowGrid').click(
    function(e){
      $('#ajInvoicePanel').hide();
      $('#ajInvoiceGrid').show();
      $('#ajInvoiceForm').remove();
      $(this).hide();
    }
  );
}


function selectInvoice(e){
  $.get(this.href,{},
    function(content){
      $('#ajInvoicePanel').show().html(content);
      $('#ajInvoiceGrid').hide();
      $('#ajInvoiceShowGrid').show();
    }
  );
  e.preventDefault();
}

function invoiceSetupSearchGridSelect(){
  $('#gridInvoiceSearch').find('a').click(selectInvoice);
}

function selectInvoiceAmounts(e){
  $.get(this.href,{},
    function(content){
      $('#formInvoiceAmounts').html(content);
    }
  );
  e.preventDefault();
}

function invoiceSetupInvoiceAmountsGridSelect(){
  $('#gridInvoiceAmounts').find('a').click(selectInvoiceAmounts);
}


function invoiceDeleteVerified(data,statusText){
  $.get(
    'invoice/createInvoiceAmounts',
    data,
    function(content){
      $('#formInvoiceAmounts').html(content);
    }
  );
  
  $.get(
    'invoice/getInvoiceAmounts',
    data,
    function(content){
      $('#gridInvoiceAmounts').html(content);
    }
  );
  
  
  getGridRowData(data.invoice_iden);
  
  hideStatusWindow();
}

function invoiceSetupVoidButton(data,prompt){
  $('#invoiceVoidButton').click(
    function(){
      $.getJSON(
        'invoice/canVoid',
        {"invoice_iden":data.invoice_iden,},
        function(canVoid){
          if(canVoid.success)
            $.get(
              'actionVerifier/verify',
              {
                'task':'invoiceVoid',
                'invoice_iden':data.invoice_iden,
                'callback':'invoiceVoidSuccess',
                'cancel_callback':'invoiceVoidSuccess({"invoice_iden":"'+data.invoice_iden+'"},\'\')',
                'prompt':prompt,
              },
              function(content){
                $('#ajInvoicePanel').html(content);
              }
            );
          else
            alert(canVoid.message);
        }
      );
    }
  );
}

function invoiceVoidSuccess(data,statusText){
  $.get(
    'invoice/edit',
    data,
    function(content){
      $('#ajInvoicePanel').html(content);
    }
  );
  getGridRowData(data.invoice_iden);
  hideStatusWindow();
}
